package xujiwen.transport.Mgr;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import xujiwen.transport.bean.ICCard;
import xujiwen.transport.util.DBConnect;

public class ICCardMgr {
	public void Save(final ICCard card, final int addYear) {
		final DBConnect db = new DBConnect();
		card.setReleaseDate(new Date());

		long tempTime=(card.getReleaseDate().getTime()/1000) + addYear*60*60*24*365;
		Date now = new Date();
		now.setTime(tempTime*1000);   
		
		card.setEffectiveDate(now);
		final String sql = "insert into ICCARD values (?, ?, ?, ?, ?)";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, card.getIcid());
			db.setString(2, card.getCid());
			db.setTimestamp(3, new Timestamp(card.getReleaseDate().getTime()));
			db.setTimestamp(4, new Timestamp(card.getEffectiveDate().getTime()));
			db.setDouble(5, card.getRemaSum());
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	
	public void Update(final Double money, final String icid) {
		final DBConnect db = new DBConnect();
		final String sql = "update ICCARD set REMASUM=? where ICID=?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setDouble(1, money);
			db.setString(2, icid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	// 判断 IC卡余额是否足够
	public boolean IsRemaSumEnough(String icid, final double charge) {
		
		Double resuma = 0.0;
		final DBConnect db = new DBConnect();
		final String sql = "select REMASUM from ICCARD where ICID=?";
		db.setString(1, icid);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				
				resuma = db.getDouble("REMASUM");

			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return false;
		} finally {
			db.close();
		}
		if(charge > resuma)
			return false;
		else
			return true;
	}
	
	public List<ICCard> GetSearchCardList(String str) {
		final List<ICCard> cardList = new ArrayList<ICCard>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from ICCARD where ICID like '%" + str +"%' or CID like '%" + str +"%'";
		else sql = "select * from ICCARD limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final ICCard card = new ICCard();
				card.setIcid(db.getString("ICID"));
				card.setCid(db.getString("CID"));
				card.setReleaseDate(db.getDate("RELEASEDATE"));
				card.setEffectiveDate(db.getDate("effectiveDate"));
				card.setRemaSum(db.getDouble("REMASUM"));
				
				cardList.add(card);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return cardList;
	}
	public void Del(final String icid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from ICCARD where ICID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, icid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
